/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iecg.api.entity.IecgCheckImg;
import com.iecg.service.IecgCheckImgService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 检验图片
 *
 * @author taohaibing
 * @date 2019-10-14 17:55:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/iecgcheckimg" )
@Api(value = "iecgcheckimg", tags = "检验图片管理")
public class IecgCheckImgController {

    private final IecgCheckImgService iecgCheckImgService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param iecgCheckImg 检验图片
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getIecgCheckImgPage(Page page, IecgCheckImg iecgCheckImg) {
		IPage<IecgCheckImg> iPage= iecgCheckImgService.page(page, Wrappers.query(iecgCheckImg));
		List<IecgCheckImg> checkImgList = iPage.getRecords();
		List<IecgCheckImg> result = checkImgList.stream().map(temp -> {
			String img= temp.getCheckImg();
			if(!img.contains("/admin/sys-file")){
				temp.setCheckImg("/admin/sys-file/iecg-"+ img);
			}
			return temp;
		}).collect(Collectors.toList());
		iPage.setRecords(result);
		return R.ok(iPage);
    }


    /**
     * 通过id查询检验图片
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(iecgCheckImgService.getById(id));
    }

    /**
     * 新增检验图片
     * @param iecgCheckImg 检验图片
     * @return R
     */
    @ApiOperation(value = "新增检验图片", notes = "新增检验图片")
    @SysLog("新增检验图片" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iecg_checkimg_add')" )
    public R save(@RequestBody IecgCheckImg iecgCheckImg) {
        return R.ok(iecgCheckImgService.save(iecgCheckImg));
    }

    /**
     * 修改检验图片
     * @param iecgCheckImg 检验图片
     * @return R
     */
    @ApiOperation(value = "修改检验图片", notes = "修改检验图片")
    @SysLog("修改检验图片" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iecg_checkimg_edit')" )
    public R updateById(@RequestBody IecgCheckImg iecgCheckImg) {
        return R.ok(iecgCheckImgService.updateById(iecgCheckImg));
    }

    /**
     * 通过id删除检验图片
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除检验图片", notes = "通过id删除检验图片")
    @SysLog("通过id删除检验图片" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iecg_checkimg_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(iecgCheckImgService.removeById(id));
    }

}
